use "study1a_data.dta", clear

gen treat=1 if Q21==1 |Q21==2
replace treat=2 if Q19==11|Q19==2
replace treat=3 if Q18==1|Q18==2
replace treat=4 if Q16==1|Q16==2
tab treat


drop if (Q20 ==1|Q20==2)
drop if (Q17 ==1|Q17==2)

***DVs

gen vote=(4-Q27)/3
tab Q27 vote
label var vote "Vote for candidate"


gen agree=1 if Q25==1
replace agree=.6667 if Q25==2
replace agree=.3333 if Q25==4
replace agree=0 if Q25==5
tab Q25 agree
label var agree "Agree with candidate"

gen truthful=(4-Q28_1)/3
tab Q28_1 truthful
label var truthful "Candidate is truthful"

gen expert=(4-Q28_2)/3
tab Q28_2 expert
label var expert "candidate is expert"

gen knowledgeable=(4-Q28_6)/3
tab Q28_6 knowledgeable
label var knowledgeable  "candidate is knowledgeable"

gen trustworthy= (4-Q28_3)/3
tab Q28_3 trustworthy
label var trustworthy "candidate is trustworthy"

gen representative=(4-Q28_8)/3
tab Q28_8 representative
label var representative "represents views of people like me"

gen shares=(4-Q28_9)/3
tab Q28_9 shares
label var shares "shares my way of thinking"

gen honest=(4-Q28_10)/3
tab Q28_10 honest
label var honest "candidate is honest"

gen pandering=(Q28_4-1)/3
tab Q28_4 pandering
label var pandering "candidate is pandering"

gen opportunistic=(Q28_5-1)/3
tab Q28_5 opportunistic
label var opportunistic "candidate is opportunistic"

gen nocare=(Q28_7-1)/3
tab Q28_7 nocare
label var nocare "Candidate doesn't care about people like me"

**Two factors in unrestricted factor analysis, restrict to two
**positive and negative load on separate dimensions
factor truthful expert knowledgeable trustworthy representative shares honest pandering opportunistic nocare, factors (2)
rotate

**Index alpha=0.934
egen competence=rowmean(truthful expert knowledgeable trustworthy representative shares honest)
label var competence "competence index"
tab competence
alpha truthful expert knowledgeable trustworthy representative shares honest



****Racial resentment index****

gen symrace1=(Q42_1-1)/3
tab Q42_1 symrace1

gen symrace2=(4-Q42_2)/3
tab Q42_2 symrace2

gen symrace3=(4-Q42_3)/3
tab Q42_3 symrace3

gen symrace4=(Q42_4-1)/3
tab Q42_4 symrace4

**alpha=0.849
alpha symrace1 symrace2 symrace3 symrace4

egen symrace=rowmean (symrace1 symrace2 symrace3 symrace4)
label var symrace "racial resentment index"

***demographics

gen income=1 if Q61==2
replace income=0 if Q61==1
tab Q61 income
label var income "income"

gen ideology=(Q69-1)/4
tab Q69 ideology
label var ideology "ideology-conservative"


gen age1829=1 if Q3==1
replace age1829=0 if Q3>1
label var age1829 "age 18-29"

gen age3044=1 if Q3==2
replace age3044=0 if Q3==1|Q3==3|Q3==4
label var age3044 "Age 30-44"

gen age4564=1 if Q3==3
replace age4564=0 if Q3==1|Q3==2|Q3==4
label var age4564 "Age 45-64"

gen age65p=1 if Q3==4
replace age65p=0 if Q3<4
label var age65p "Age 65+"

gen female=1 if Q5==1
replace female=0 if Q5==2
label var female "female"


gen pid7 = 0 if Q68==3
replace pid7 = -3 if Q67==1
replace pid7 = -2 if Q67==2
replace pid7 = -1 if Q68==1
replace pid7 = 1 if Q68==2
replace pid7 = 2 if Q66==2
replace pid7 = 3 if Q66==1

gen pid7R = pid7/3

gen college = 1 if Q57>=3
*gen income = Q63 + 5
*replace income = Q62 if income==.

gen FT_blacks = Q35/100
gen discrim_blacks = (4-Q40_2)/3
gen inf_black = 2-Q43

gen perceived_Dem = 2-Q55


save "study1a_cleaned.dta", replace

use "study1b_data.dta", clear

***CLEAN VARIABLES***

gen treatment=1 if Q22==1|Q22==2
replace treatment=2 if Q20==1|Q20==2
replace treatment=3 if Q21==1|Q21==3
replace treatment=4 if Q19==1|Q19==2
replace treatment=5 if Q17==1|Q17==2
replace treatment=6 if Q18==1|Q18==2
tab treatment


drop if treatment ==3|treatment==6

gen treat = treatment
recode treat 1=1 2=2 4=3 5=4

***DVs

gen vote=(4-Q27)/3
tab Q27 vote
label var vote "Vote for candidate"


gen agree=1 if Q25==1
replace agree=.6666 if Q25==2
replace agree=.3333 if Q25==4
replace agree=0 if Q25==5
tab Q25 agree
label var agree "Agree with candidate"

gen truthful=(4-Q28_1)/3
tab Q28_1 truthful
label var truthful "Candidate is truthful"

gen expert=(4-Q28_2)/3
tab Q28_2 expert
label var expert "candidate is expert"

gen knowledgeable=(4-Q28_6)/3
tab Q28_6 knowledgeable
label var knowledgeable  "candidate is knowledgeable"

gen trustworthy= (4-Q28_3)/3
tab Q28_3 trustworthy
label var trustworthy "candidate is trustworthy"

gen representative=(4-Q28_8)/3
tab Q28_8 representative
label var representative "represents views of people like me"

gen shares=(4-Q28_9)/3
tab Q28_9 shares
label var shares "shares my way of thinking"

gen honest=(4-Q28_10)/3
tab Q28_10 honest
label var honest "candidate is honest"

gen pandering=(Q28_4-1)/3
tab Q28_4 pandering
label var pandering "candidate is pandering"

gen opportunistic=(Q28_5-1)/3
tab Q28_5 opportunistic
label var opportunistic "candidate is opportunistic"

gen nocare=(Q28_7-1)/3
tab Q28_7 nocare
label var nocare "Candidate doesn't care about people like me"

**Two factors in unrestricted factor analysis, restrict to two
**positive and negative load on separate dimensions
factor truthful expert knowledgeable trustworthy representative shares honest pandering opportunistic nocare, factors (2)
rotate

**Index alpha=0.934
egen competence=rowmean(truthful expert knowledgeable trustworthy representative shares honest)
label var competence "competence index"
tab competence
alpha truthful expert knowledgeable trustworthy representative shares honest


****Racial resentment index****

gen symrace1=(Q44_1-1)/3
tab Q44_1 symrace1

gen symrace2=(4-Q44_2)/3
tab Q44_2 symrace2

gen symrace3=(4-Q44_3)/3
tab Q44_3 symrace3

gen symrace4=(Q44_4-1)/3
tab Q44_4 symrace4

**alpha=0.87
alpha symrace1 symrace2 symrace3 symrace4

egen symrace=rowmean (symrace1 symrace2 symrace3 symrace4)
label var symrace "racial resentment index"

***demographics

gen income=1 if Q59==2
replace income=0 if Q59==1
tab Q59 income
label var income "income"

gen ideology=(Q67-1)/4
tab Q67 ideology
label var ideology "ideology-conservative"


gen age1829=1 if Q3==1
replace age1829=0 if Q3>1
label var age1829 "age 18-29"

gen age3044=1 if Q3==2
replace age3044=0 if Q3==1|Q3==3|Q3==4
label var age3044 "Age 30-44"

gen age4564=1 if Q3==3
replace age4564=0 if Q3==1|Q3==2|Q3==4
label var age4564 "Age 45-64"

gen age65p=1 if Q3==4
replace age65p=0 if Q3<4
label var age65p "Age 65+"


gen female = 2-Q5
gen college = 1 if Q55>=3
replace college = 0 if Q55 <3
gen age = 2015-Q56

gen FT_blacks = Q38/100
gen discrim_blacks = (4-Q42_2)/3
gen inf_black = 2-Q45

gen pid7 = 0 if Q66==3
replace pid7 = -3 if Q65==1
replace pid7 = -2 if Q65==2
replace pid7 = -1 if Q66==1
replace pid7 = 1 if Q66==2
replace pid7 = 2 if Q64==2
replace pid7 = 3 if Q64==1

gen pid7R = pid7/3

gen perceived_Dem = 2-Q53

append using "study1a_cleaned.dta"



***Additional traits ***
drop honest shares expert
gen truth = (4-Q28_1)/3
gen expert = (4-Q28_2)/3
gen trust = (4-Q28_3)/3
gen pander = (4-Q28_4)/3
gen opportun = (4-Q28_5)/3
gen know = (4-Q28_6)/3
gen care = (4-Q28_7)/3
gen represent = (4-Q28_8)/3
gen shares = (4-Q28_9)/3
gen honest = (4-Q28_10)/3


*gen treatDRR = treatDerog * symrace
*reg agree i.treat symrace treatBRR treatDRR

alpha vote agree, item
gen index = (vote + agree)/2

alpha vote agree truthful trustworthy honest, i
gen indexA = (vote+agree+truthful+trustworthy+honest)/5

*alpha truth expert trust pandering opportun know care represent shares honest, i g(indexChar)
alpha truth trust honest, i gen(indexChar)
alpha pander care opportun represent shares, i 
gen indexApp2 = ((1-pander) + (1-care) + (1-opportun) + represent + shares)/5
alpha pander opportun, i gen(indexApp)
gen ageR = (age -20)/55
gen incomeR = (income-1)/9

label var age "Age"
label var ageR "Age"
label var female "Female"
label var college "College Educated"
label var incomeR "Family Income"
label var ideology "Ideology"
label var symrace "Racial Resentment"
label var treat "Treatment"

label var index "Approval"
label var indexChar "Character"
label var indexApp2 "Appropriate"

label define treat 1 "White Neutral" 2 "White Derogator" 3 "Black Neutral" 4 "Black Derogator"
label values treat treat


save "study1_cleaned.dta", replace

*Balance and Demographics Tables
set matsize 2000
cd "./tables" 

estpost summarize age female college incomeR pid7R ideology symrace  
esttab using study1_summary.tex, cells("count mean sd min max") label noobs replace 


estpost summarize index indexApp2 indexChar  
esttab using study1_dvs.tex, cells("count mean sd min max") label noobs replace 


***** Summary Stats of DVs by Party
orth_out ageR female college incomeR pid7R ideology symrace using study1_balance.tex, by(treat) latex se pcompare count replace
